<?php
use app\assets\AuctionListAsset;
use yii\helpers\Html;

AuctionListAsset::register($this);
?>

<div id="warehouseToolbar">
    <button id="remove" class="btn btn-danger" disabled>
        <i class="glyphicon glyphicon-remove"></i> 批量删除
    </button>
    <button id="shelf" class="btn btn-default" disabled>
        批量上架
    </button>
</div>

<table id="warehouseTable">
</table>

<!--删除Modal-->
<div class="modal fade" id="removeModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <!--记录-->
                <span id="remove_id" hidden></span>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">确定要删除该商品吗？</h4>
            </div>
            <div class="modal-body">
                <p style="color: #FF3333">删除后该商品将在数据库中移除,请谨慎操作。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="remove_btn">确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>

    //初始化表格设置
    $('#warehouseTable').bootstrapTable({
        height: 800,//高度
        toolbar: '#warehouseToolbar',        //工具按钮用哪个容器
//        clickToSelect: true,//点击行即可选中单选/复选框
        singleSelect: false,//复选框只能选择一条记录
        search: true,//是否显示右上角的搜索框
        striped: true,      //是否显示行间隔色
        cache: false,            //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,     //是否显示分页（*）
        sortable: false,           //是否启用排序
        sortOrder: "asc",          //排序方式
        sidePagination: "server", //服务端处理分页
        method: 'get',      //请求方式（*）
        pageNumber:1,      //初始化加载第一页，默认第一页
        pageSize: 10,      //每页的记录行数（*）
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        cardView: false,          //是否显示详细视图
        uniqueId:'commodity_id',
        url:"warehouse-table-data",

        columns: [{
            field: 'state',
            checkbox: true,
            align: 'center',
            width:'3%',
            valign: 'middle'
        },{
            field: '',
            align: 'center',
            title: '主图',
            width:'15%',
            valign: 'middle',
            formatter:function(value,row,index){
                var b = JSON.parse(row.banner_url);
                var a = '<img src="'+b[0]+'"'+'alt="商品首图" style="width: 100px;height: 80px">';
                return a;}
        }, {
            field: 'commodity_name',
            align: 'center',
            valign: 'middle',
            width:'35%',
            title: '名称'
        },{
            field: 'price',
            align: 'center',
            valign: 'middle',
            width:'10%',
            title: '价格',
            formatter:function(value,row,index){
                var a = '<span style="color: #f60;margin: 0 0 0px">'+'¥ '+row.price+'</span>'
                return a;
            }
        },{
            field: 'stock_num',
            align: 'center',
            valign: 'middle',
            width:'7%',
            title: '库存'
        },{
            field: 'sales_num',
            align: 'center',
            valign: 'middle',
            width:'7%',
            title: '销量'
        },{
            field: 'create_time',
            align: 'center',
            valign: 'middle',
            width:'10%',
            title: '发布时间'
        },{
            field: 'operate',
            align: 'center',
            valign: 'middle',
            width:'18%',
            title: '操作',
            formatter:function(value,row,index){
                var a = '<a href="#" mce_href="#" onclick="onSaleClick(\''+ row.commodity_id + '\',\'' +index+ '\')">上架</a> ';
                var b = '<a href="#" mce_href="#" onclick="onRemoveClick(\''+ row.commodity_id + '\',\'' +index+ '\')">删除</a> ';
                var c = '<a href="../publish/edit-commodity?commodity_id='+row.commodity_id+'"'+'>编辑</a> ';

                return c+a+b;}
        }]
    });

    $(window).resize(function () {
        $('#warehouseTable').bootstrapTable('resetView');
    });

    //复选框点击事件
    $('#warehouseTable').on('check.bs.table uncheck.bs.table ' +
        'check-all.bs.table uncheck-all.bs.table', function () {
        $('#remove').prop('disabled', !$('#warehouseTable').bootstrapTable('getSelections').length);
        $('#shelf').prop('disabled', !$('#warehouseTable').bootstrapTable('getSelections').length);
        selections = getIdSelections();

    });

    //获取选择的合集
    function getIdSelections() {
        return $.map($('#warehouseTable').bootstrapTable('getSelections'), function (row) {
            return row.commodity_id;
        });
    }

    //批量删除操作
    $('#remove').click(function () {
        var ids = getIdSelections();
        console.log('ids:'+ids);

        $.ajax({
            url: "remove-commodity",
            data:{"commodity_id":ids},
            dataType: "json",
            success: function (data) {

                $('#warehouseTable').bootstrapTable('remove', {
                    field: 'commodity_id',
                    values: ids
                });
                $('#remove').prop('disabled', true);
                alert('批量删除成功');
            }
        });

    });

    //批量上架操作
    $('#shelf').click(function () {
        var ids = getIdSelections();
        console.log('ids:'+ids);
        $.ajax({
            url: "on-sale",
            data:{"commodity_id":ids},
            dataType: "json",
            success: function (data) {
                $('#warehouseTable').bootstrapTable('remove', {
                    field: 'commodity_id',
                    values: ids
                });
                $('#shelf').prop('disabled', true);
                alert('批量上架成功');
            }
        });

    });

    //点击下架按钮
    function onSaleClick(commodity_id,index){

        console.log(commodity_id);
        //
        $.ajax({
            url: "on-sale",
            data:{"commodity_id":commodity_id},
            dataType: "json",
            success: function (data) {
                $('#warehouseTable').bootstrapTable('removeByUniqueId', commodity_id);
                alert('上架成功');
            }
        });

    }

    //点击表格中删除a标签
    function onRemoveClick(commodity_id,index){

        $('#remove_id').html(commodity_id);
        $('#removeModal').modal();

    }

    $('#remove_btn').click(function(){

        $.ajax({
            url: "remove-commodity",
            data:{"commodity_id":$('#remove_id').html()},
            dataType: "json",
            success: function (data) {

                $('#warehouseTable').bootstrapTable('removeByUniqueId',$('#remove_id').html());
                $('#removeModal').modal('hide');
                alert('删除成功');
            }
        });
    });


</script>
